package org.rpgmapper.view;

import org.rpgmapper.R;
import org.rpgmapper.bo.Character;
import org.rpgmapper.bo.Hero;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import org.rpgmapper.bo.Stat;


public class CharacterView extends LinearLayout {

	private View view;
	private OnClickListener btnOKClick;
	private OnClickListener btnCancelClick;
	private Character character = null;
	
	public CharacterView(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.character_dialog, this);
		init();
	}

	public CharacterView(Context context, Character character) {
		this(context);
		this.character = character;
		
		((TextView)findViewById(R.id.name)).setText(this.character.getName());

		((TextView)findViewById(R.id.hitPoints)).setText(this.character.get(Stat.MaxHitPoints)+ " max hitpoints");
		((SeekBar)findViewById(R.id.seekBarHitPoints)).setProgress(this.character.get(Stat.MaxHitPoints));

		((TextView)findViewById(R.id.movePoints)).setText(this.character.get(Stat.MovePoints)+ " max movepoints");
		((SeekBar)findViewById(R.id.seekBarMovePoints)).setProgress(this.character.get(Stat.MovePoints));

		((TextView)findViewById(R.id.AC)).setText(this.character.get(Stat.AC)+ " AC");
		((SeekBar)findViewById(R.id.seekBarAC)).setProgress(this.character.get(Stat.AC));
		
		((NumberPicker)findViewById(R.id.money)).setMinValue(0);
		((NumberPicker)findViewById(R.id.money)).setMaxValue(1000000);
		((NumberPicker)findViewById(R.id.money)).setValue(this.character.get(Stat.Money));
	}

	public CharacterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.character_dialog, this);
		//System.err.println("on construit 2");
		this.addView(view);
		
		init();
	}
	
	private void init() {
		((SeekBar)findViewById(R.id.seekBarHitPoints)).setOnSeekBarChangeListener(onSeekBarHitPointsChange);
		((SeekBar)findViewById(R.id.seekBarMovePoints)).setOnSeekBarChangeListener(onSeekBarMovePointsChange);
		((SeekBar)findViewById(R.id.seekBarAC)).setOnSeekBarChangeListener(onSeekBarACChange);
		//((SeekBar)findViewById(R.id.seekBarHitPoints)).setIndeterminate(false);
		//((SeekBar)findViewById(R.id.seekBarHitPoints)).setProgress(60);
		this.setBtnCancelClick(new OnClickListener() {
			public void onClick(View v) {
				((ViewGroup)CharacterView.this.getParent()).removeView(CharacterView.this);
			}
		});
	}

	private OnSeekBarChangeListener onSeekBarHitPointsChange = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			((TextView)findViewById(R.id.hitPoints)).setText(progress+" max hitpoints");
		}
	};
	private OnSeekBarChangeListener onSeekBarACChange = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			((TextView)findViewById(R.id.AC)).setText(progress+" AC");
		}
	};
	private OnSeekBarChangeListener onSeekBarMovePointsChange = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
		public void onStartTrackingTouch(SeekBar seekBar) {
		}
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			((TextView)findViewById(R.id.movePoints)).setText(progress+" max movepoints");
		}
	};
	
	public Character getUpdatedCharacter() {
		Character c = new Hero();
		this.character.copyTo(c);
		
		c.setName(((TextView)findViewById(R.id.name)).getText().toString());
		c.setBase(Stat.MaxHitPoints,((SeekBar)findViewById(R.id.seekBarHitPoints)).getProgress());
		c.setBase(Stat.MovePoints,((SeekBar)findViewById(R.id.seekBarMovePoints)).getProgress());
		c.setBase(Stat.AC,((SeekBar)findViewById(R.id.seekBarAC)).getProgress());
		c.setBase(Stat.Money,((NumberPicker)findViewById(R.id.money)).getValue());
		
		return c;
	}
	
	public OnClickListener getBtnOKClick() {
		return btnOKClick;
	}

	public void setBtnOKClick(OnClickListener btnOKClick) {
		this.btnOKClick = btnOKClick;
		findViewById(R.id.ok).setOnClickListener(this.btnOKClick);
	}

	public OnClickListener getBtnCancelClick() {
		return btnCancelClick;
	}

	public void setBtnCancelClick(OnClickListener btnCancelClick) {
		this.btnCancelClick = btnCancelClick;
		findViewById(R.id.cancel).setOnClickListener(this.btnCancelClick);
	}

	public Character getCharacter() {
		return character;
	}

	public void setCharacter(Character character) {
		this.character = character;
	}


}
